package com.haojiang.mh.widget.chart;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import com.haojiang.mh.R;
import com.haojiang.mh.service.entity.DayScoreBean;
import com.haojiang.mh.utils.ImageUtil;
import com.haojiang.mh.utils.SizeUtil;
import com.haojiang.mh.widget.OnScoreBallClickListener;

/**
 * 日历每天对应数据详情View
 */
public class DayTextView extends View {

    private TextPaint textPaint;//文本画笔
    private Paint layerPaint;//图层画笔
    //低分、中等分、高分、工作日、周末、其他月份 颜色值
    private int color_low, color_mid, color_hig, work_day_color, weekend_color, other_month_color;
    private int dayTextColor, scoreTextColor, layerColor;//颜色值
    private int dayTextSize, scoreTextSize;//字体大小
    private int day, score = -1;//文本
    private int layerRadius;//layer半径
    private int width, height, defaultWidth, defaultHeight;//当前View宽高、默认宽高
    private Bitmap bestStar;//得分最高
    private boolean isBestScore = false;
    private OnScoreBallClickListener mOnScoreBallClickListener;

    public DayTextView(Context context) {
        this(context, null);
    }

    public DayTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DayTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        @SuppressLint("CustomViewStyleable")
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SleepEvaluateCalendar, defStyleAttr, 0);
        dayTextColor = a.getColor(R.styleable.SleepEvaluateCalendar_day_color, getResources().getColor(R.color.white));
        scoreTextColor = a.getColor(R.styleable.SleepEvaluateCalendar_score_color, getResources().getColor(R.color.white));
        dayTextSize = a.getDimensionPixelSize(R.styleable.SleepEvaluateCalendar_day_size, SizeUtil.sp2px(context, 12));
        scoreTextSize = a.getDimensionPixelSize(R.styleable.SleepEvaluateCalendar_score_size, SizeUtil.sp2px(context, 13));
        color_hig = a.getColor(R.styleable.SleepEvaluateCalendar_layer_high_color, getResources().getColor(R.color.purple_3));
        color_mid = a.getColor(R.styleable.SleepEvaluateCalendar_layer_mid_color, getResources().getColor(R.color.purple_5));
        color_low = a.getColor(R.styleable.SleepEvaluateCalendar_layer_low_color, getResources().getColor(R.color.grey_3));
        layerRadius = a.getInt(R.styleable.SleepEvaluateCalendar_layer_radius, SizeUtil.dp2px(context, 13));
        int bestStarId = a.getResourceId(R.styleable.SleepEvaluateCalendar_best_star, R.drawable.ic_best_star);
        a.recycle();

        work_day_color = getResources().getColor(R.color.white);
        weekend_color = getResources().getColor(R.color.purple_3);
        other_month_color = getResources().getColor(R.color.white_half);

        layerColor = color_mid;
        defaultWidth = SizeUtil.dp2px(context, 48);
        defaultHeight = SizeUtil.dp2px(context, 67);
        bestStar = ImageUtil.getBitmapFromDrawable(context, bestStarId);

        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextAlign(Paint.Align.CENTER);

        layerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        layerPaint.setStyle(Paint.Style.FILL);
    }

    public void setOnScoreBallClickListener(OnScoreBallClickListener onScoreBallClickListener) {
        mOnScoreBallClickListener = onScoreBallClickListener;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        textPaint.setColor(dayTextColor);
        textPaint.setTextSize(dayTextSize);
        canvas.drawText(
                String.valueOf(day),
                width * 0.5f,
                SizeUtil.getBaseLine(textPaint, height * 0.5f, height * 0.5f),
                textPaint
        );

        if (score == -1) return;
        layerPaint.setColor(layerColor);
        canvas.drawCircle(width * 0.5f, (float) height * 3 / 4, layerRadius, layerPaint);
        if (isBestScore) {
            canvas.drawBitmap(bestStar, width * 0.5f - layerRadius, (float) height * 3 / 4 - layerRadius, null);
        }
        textPaint.setColor(scoreTextColor);
        textPaint.setTextSize(scoreTextSize);
        canvas.drawText(
                String.valueOf(score),
                width * 0.5f,
                SizeUtil.getBaseLine(textPaint, height, height * 0.5f),
                textPaint
        );
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        final int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if ((widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) ||
                (widthSpecMode == MeasureSpec.UNSPECIFIED && heightSpecMode == MeasureSpec.UNSPECIFIED)) {
            setMeasuredDimension(defaultWidth, defaultHeight);
        } else if (widthSpecMode == MeasureSpec.AT_MOST || widthSpecMode == MeasureSpec.UNSPECIFIED) {
            setMeasuredDimension(defaultWidth, heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
            setMeasuredDimension(widthSpecSize, defaultHeight);
        }
    }

    public void setDayTextColor(int dayTextColor) {
        this.dayTextColor = dayTextColor;
        invalidate();
    }

    public void setScoreTextColor(int scoreTextColor) {
        this.scoreTextColor = scoreTextColor;
        invalidate();
    }

    public void setTextColors(int dayTextColor, int scoreTextColor) {
        this.dayTextColor = dayTextColor;
        this.scoreTextColor = scoreTextColor;
        invalidate();
    }

    public void setDayColors(int workDayColor, int weekendColor, int otherMonthColor) {
        this.work_day_color = workDayColor;
        this.weekend_color = weekendColor;
        this.other_month_color = otherMonthColor;
        invalidate();
    }

    public void setDayTextSize(int dayTextSize) {
        this.dayTextSize = dayTextSize;
    }

    public void setScoreTextSize(int scoreTextSize) {
        this.scoreTextSize = scoreTextSize;
    }

    public void setTextSizes(int dayTextSize, int scoreTextSize) {
        this.dayTextSize = dayTextSize;
        this.scoreTextSize = scoreTextSize;
        invalidate();
    }

    public void setDay(int day) {
        this.day = day;
        invalidate();
    }

    public void setScore(int score) {
        this.score = score;
        getLayerColor();
        invalidate();
    }

    public void setBestStart(Bitmap bestStar) {
        this.bestStar = bestStar;
        invalidate();
    }

    public void setBestScore(boolean bestScore) {
        isBestScore = bestScore;
        invalidate();
    }

    public void setLayerRadius(int layerRadius) {
        this.layerRadius = layerRadius;
        invalidate();
    }

    private void setText(int day, int score, boolean isWeekend, boolean isCurrMonth, boolean isBestScore) {
        this.day = day;
        this.score = score;
        this.isBestScore = isBestScore;
        dayTextColor = isCurrMonth ? (isWeekend ? weekend_color : work_day_color) : other_month_color;
        layerColor = getLayerColor();
        invalidate();
    }

    /**
     * 获取layer颜色
     */
    public int getLayerColor() {
        int layerColor;
        if (score > 85) {
            layerColor = color_hig;
        } else if (score >= 75) {
            layerColor = color_mid;
        } else {
            layerColor = color_low;
        }
        return layerColor;
    }

    public void setLayerColor(int layerColor) {
        this.layerColor = layerColor;
        invalidate();
    }

    public void setDayAndScore(final DayScoreBean ds) {
        if (ds == null) return;
        setText(ds.getDay(), ds.getScore(), ds.isWeekend(), ds.isCurrMonth(), ds.isBestScore());
        if (ds.getScore() > 0 && mOnScoreBallClickListener != null) {
            setOnClickListener(v -> mOnScoreBallClickListener.onClick(ds.getDate()));
        }
    }
}